//
//  RVGuidePageViewController.m
//  RememberVocabularies
//
//  Created by 伍银荣 on 2017/10/10.
//  Copyright © 2017年 WYR. All rights reserved.
//

#import "RVGuidePageViewController.h"
#import "RVTabBarController.h"

@interface RVGuidePageViewController ()<UIScrollViewDelegate>

@property (nonatomic,strong) UIScrollView *scrollView;
@property (nonatomic,strong) UIPageControl *pageControl;
//进入按钮
@property (nonatomic,strong) UIButton *entranceBtn;

@end

@implementation RVGuidePageViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupUI];
}

-(void)setupUI
{
    for (int i = 0; i < 3; i++) {
        NSString *imageName = [NSString stringWithFormat:@"welcome%02d",i+1];
        UIImageView *imageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:imageName]];
        imageView.frame = CGRectMake(i * Width, 0, Width, Height);
        [self.scrollView addSubview:imageView];
        
    }
    [self.view addSubview:self.scrollView];
    [self.view addSubview:self.pageControl];
    [self.view addSubview:self.entranceBtn];
    
    
    
}

-(UIScrollView *)scrollView
{
    if (!_scrollView) {
        UIScrollView *scrollView = [[UIScrollView alloc]init];
        scrollView.backgroundColor = [UIColor whiteColor];
        scrollView.frame = [UIScreen mainScreen].bounds;
        scrollView.bounces = NO;
        scrollView.showsVerticalScrollIndicator = NO;
        scrollView.showsHorizontalScrollIndicator = NO;
        scrollView.pagingEnabled = YES;
        [scrollView setMaximumZoomScale:2.0f];
        [scrollView setMinimumZoomScale:0.2f];
        scrollView.delegate = self;
        scrollView.contentSize = CGSizeMake(Width * 3, Height);
        _scrollView = scrollView;
    }
    return _scrollView;
}



-(UIPageControl *)pageControl
{
    if (!_pageControl) {
        //分页控件与scrollView无任何关系，是两个独立的控件
        UIPageControl *pageControl = [[UIPageControl alloc]init];
        pageControl.numberOfPages = 3;
        //控件尺寸
        CGSize size = [pageControl sizeForNumberOfPages:pageCount];
        pageControl.bounds = CGRectMake(0, 0, size.width, size.height);
        pageControl.center = CGPointMake(self.view.center.x, Height - 120);
        pageControl.currentPage = 0;
        
        pageControl.currentPageIndicatorTintColor = RVGlobalColor ;
        pageControl.pageIndicatorTintColor = [UIColor groupTableViewBackgroundColor];
        [pageControl addTarget:self action:@selector(updateScrollPage) forControlEvents:UIControlEventValueChanged];
        _pageControl = pageControl;
    }
    return _pageControl;
}

-(UIButton *)entranceBtn
{
    if (!_entranceBtn) {
        UIButton *entranceBtn = [[UIButton alloc]init];
        entranceBtn.frame = CGRectMake(0, 0, 120, 40);
        entranceBtn.hidden = YES;
        entranceBtn.center = _pageControl.center;
        [entranceBtn setTitle:@"进入" forState:UIControlStateNormal];
        [entranceBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [entranceBtn setBackgroundColor:RVGlobalColor];
        entranceBtn.layer.cornerRadius = 20;
        [entranceBtn.layer setMasksToBounds:YES];
        [entranceBtn addTarget:self action:@selector(entranceBtnClick) forControlEvents:UIControlEventTouchUpInside];
        _entranceBtn = entranceBtn;
    }
    return _entranceBtn;
}




-(void)updateScrollPage
{
    CGPoint setoffPoint = CGPointMake(_pageControl.currentPage*self.view.bounds.size.width, 0);
    [_scrollView setContentOffset:setoffPoint animated:YES];
    
}

//滚动视图停下来，修改页面控件的小点（页数）
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    //计算页数
    NSInteger currentPage = _pageControl.currentPage = _scrollView.contentOffset.x/self.view.bounds.size.width;
    NSLog(@"第%ld页",currentPage+1);
    if (currentPage == 2) {
        [_pageControl setHidden:YES];
        [self.entranceBtn setHidden:NO];
    }else{
        [_pageControl setHidden:NO];
        [self.entranceBtn setHidden:YES];
    }
    
}

-(void)entranceBtnClick
{
    RVTabBarController *tabBarC = [[RVTabBarController alloc]init];
    [self presentViewController:tabBarC animated:YES completion:nil];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
